package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    DtsRoleMapper roleMapper;

    @Override
    public Set<String> findRolesByIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DtsRole::getId, roleIds);
        wrapper.eq(DtsRole::getDeleted, false);

        List<DtsRole> roles = roleMapper.selectList(wrapper);

//        Set<String> set = new HashSet<>();
//        for (DtsRole role : roles) {
//            set.add(role.getName());
//        }
        //map （） 映射
        //collect() 指定返回值集合类型
        Set<String> set = roles.stream().map(role -> {
            return role.getName();
        }).collect(Collectors.toSet());

        return set;
    }

    @Override
    public List<DtsRole> findAll() {

        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsRole::getDeleted, false);

        List<DtsRole> roles = roleMapper.selectList(wrapper);

        return roles;
    }

    @Override
    public IPage<DtsRole> queryPage(Integer page, Integer limit, String sort, String order, String rolename) {

        IPage<DtsRole> ipage = new Page<>(page , limit);

        QueryWrapper<DtsRole> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(rolename)){
            wrapper.like("name", rolename );
        }

        wrapper.eq("deleted", false);

        if(order.equalsIgnoreCase("asc")){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }

        ipage = roleMapper.selectPage(ipage, wrapper);

        return ipage;
    }

    @Override
    public DtsRole add(DtsRole role) {

        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        role.setDeleted(false);
        role.setEnabled(true);

        roleMapper.insert(role);
        return role;
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public void update(DtsRole role) {
        role.setUpdateTime(new Date());
        roleMapper.updateById(role);
    }

    @Autowired
    DtsPermissionMapper permissionMapper;;
    @Override
    public Set<String> findRoleStrsById(Integer roleId) {
        LambdaQueryWrapper<DtsPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsPermission::getDeleted, false);
        wrapper.eq(DtsPermission::getRoleId, roleId);

        List<DtsPermission> permissions = permissionMapper.selectList(wrapper);
        return permissions.stream().map(dtsPermission -> {
            return dtsPermission.getPermission();
        }).collect(Collectors.toSet());
    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsRole::getDeleted, false);
        wrapper.eq(DtsRole::getId,roleId);

        DtsRole role = roleMapper.selectOne(wrapper);

        if(role.getName().equalsIgnoreCase("超级管理员")){
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public void updatePermission(int roleId, String[] permissions) {
        //1 删除该角色原有的权限
        LambdaQueryWrapper<DtsPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsPermission::getRoleId, roleId);
        permissionMapper.delete(wrapper);


        //2. 添加该角色新的权限
        for (String permission : permissions) {
            DtsPermission perm  = new DtsPermission();
            perm.setRoleId(roleId);
            perm.setDeleted(false);
            perm.setUpdateTime(new Date());
            perm.setAddTime(new Date());
            perm.setPermission(permission);


            permissionMapper.insert(perm);
        }
    }
}
